package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.service.AdminAuthService;
import com.cskaoyan.market.service.AdminAuthServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/admin/auth/*")
public class AdminAuthController extends HttpServlet {
    AdminAuthService adminAuthService = new AdminAuthServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI=req.getRequestURI();
        String op =requestURI.replace(req.getContextPath()+"/admin/auth/","");
        if("login".equals(op)){
            login(req,resp);
        }else {
            if("logout".equals(op)){
                logout(req,resp);
            }
        }
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    //接收用户提交过来的数据，进行校验，返回结果
        String requestBody=req.getReader().readLine();
        String username= JacksonUtil.parseString(requestBody,"username");
        String password= JacksonUtil.parseString(requestBody,"password");
        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            Object fail= ResponseUtil.fail(601,"用户账户或密码为空");
            String s=JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(s);
            return;
        }
        MarketAdmin admin = adminAuthService.login(username,password);
        if(admin!=null){
            HttpSession session =req.getSession();
            session.setAttribute("admin",admin);

            Map<String,Object> data=new HashMap<>();
            Map<String,String> adminInfo=new HashMap<>();
            adminInfo.put("nickName",admin.getUsername());
            adminInfo.put("avatar",admin.getAvatar());
            data.put("adminInfo",adminInfo);
            data.put("token",session.getId());
            Object ok =ResponseUtil.ok(data);
            String s=JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().println(s);
        }else{
            Object fail =ResponseUtil.fail(605,"用户账号或密码不正确");
            String s=JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().println(s);
        }
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestURI= req.getRequestURI();
        String op =requestURI.replace(req.getContextPath()+"/admin/auth/","");
        if("info".equals(op)){
            info(req,resp);
        }
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session=req.getSession();
        session.invalidate();
        Object fail =ResponseUtil.fail(0,"成功");
        String s=JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().println(s);
    }

    public void info(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        HttpSession session =req.getSession();
        MarketAdmin admin = (MarketAdmin) session.getAttribute("admin");
        Map<String,Object> data= new HashMap<>();
        data.put("roles", Arrays.asList("超级管理员"));
        data.put("name",admin.getUsername());
        data.put("avatar",admin.getAvatar());
        data.put("perms",Arrays.asList("*"));
        Object ok=ResponseUtil.ok(data);
        String s=JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(s);


    }
}
